﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IDAO;
using CNKI.TPI.Web.Search.Model;
using System.Linq.Expressions;
using KNet.Data.Entity;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;

namespace CNKI.TPI.Web.Search.DAO
{
    class CategoryDAO : BaseDAO, ICategoryDAO
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBName"></param>
        /// <returns></returns>
        public IList<CLS> GetCategroyType(string DBName, int DBID)
        {
            IList<CLS> result = new List<CLS>();
            try
            {
                string strwhere = "";
                if (SessionHelper.IsUserInfoExsit())
                {
                    string catname = GetNavFirstName(DBID);
                    if (catname != null && catname != "")
                    {
                        if ("-1" == catname)
                        {
                            return result;
                        }
                        catname = catname.TrimEnd(',');
                        strwhere = " where NAME=(" + catname.Replace(',', '+') + ")";
                    }
                }

                string sql = "select TEMPLATECODE,NAME,EXPRESSION,AHEADMATCH,REFCLASSFLAG,SORTNUMBER,SYSID from {0}_CLS {1} order by SORTNUMBER";
                result = Context.ExecuteSql<CLS>(string.Format(sql, DBName, strwhere));
                if (result != null && result.Count > 0)
                {
                    foreach (CLS c in result)
                    {
                        c.Name = StringHelper.RemoveHighlightFlag(c.Name);
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        public IList<CLS> GetCategroyTypeAll(string DBName)
        {
            IList<CLS> result = new List<CLS>();
            try
            {
                string sql = "select TEMPLATECODE,NAME,EXPRESSION,AHEADMATCH,REFCLASSFLAG,SORTNUMBER,SYSID from {0}_CLS  order by SORTNUMBER";
                result = Context.ExecuteSql<CLS>(string.Format(sql, DBName));
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBName"></param>
        /// <param name="sysID"></param>
        /// <returns></returns>
        public CLS GetCategroyTypeBySysID(string DBName, int sysID)
        {
            CLS result = new CLS();
            try
            {
                string sql = "select TEMPLATECODE,NAME,EXPRESSION,AHEADMATCH,REFCLASSFLAG,SORTNUMBER,SYSID from {0}_CLS where SYSID='{1}' order by SORTNUMBER";
                IList<CLS> itemList = Context.ExecuteSql<CLS>(string.Format(sql, DBName, sysID));
                if (itemList != null && itemList.Count > 0)
                {
                    result = itemList[0];
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBName"></param>
        /// <param name="categoryType"></param>
        /// <returns></returns>
        public CLS_NODE GetCategroy(string DBName, int categoryType, string code)
        {
            CLS_NODE result = null;
            try
            {
                string sql = "select CLSID,NAME,CODE,GRADE,CHILDFLAG,SYSCODE,SORTNUMBER,SYSID from {0}_CLS_NODE where CLSID = '{1}' and code='{2}'";
                IList<CLS_NODE> nodeList = Context.ExecuteSql<CLS_NODE>(string.Format(sql, DBName, categoryType, code));
                if (nodeList != null && nodeList.Count > 0)
                {
                    result = nodeList[0];
                    result.Code = StringHelper.RemoveHighlightFlag(result.Code);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public IList<CLS_NODE> GetCategroy(string DBName, int categoryType, IList<string> syscode)
        {
            IList<CLS_NODE> result = null;
            try
            {
                if (syscode == null || syscode.Count == 0)
                {
                    return null;
                }

                string sql = "select CLSID,NAME,CODE,GRADE,CHILDFLAG,SYSCODE,SORTNUMBER,SYSID from {0}_CLS_NODE where CLSID = '{1}'";
                sql = string.Format(sql, DBName, categoryType);
                foreach (string item in syscode)
                {
                    sql += " and SYSCODE = '" + item + "'";
                }
                IList<CLS_NODE> nodeList = Context.ExecuteSql<CLS_NODE>(sql+ " order by SYSCODE");
                if (nodeList != null && nodeList.Count > 0)
                {
                    result = nodeList;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBName"></param>
        /// <param name="categoryType"></param>
        /// <returns></returns>
        public IList<CLS_NODE> GetCategroyList(string DBName, int categoryType, int DBID)
        {
            IList<CLS_NODE> result = new List<CLS_NODE>();
            string catcode = null;
            try
            {
                string strwhere = "";
                if (SessionHelper.IsUserInfoExsit())
                {
                    catcode = GetNavChildCode(categoryType.ToString(), DBID);
                    if (catcode != null && catcode != "")
                    {
                        if ("-1" == catcode)
                        {
                            return result;
                        }
                        strwhere = " and CODE=" + catcode.Replace(',', '+');
                    }
                }

                string sql = "select CLSID,NAME,CODE,GRADE,CHILDFLAG,SYSCODE,SORTNUMBER,SYSID from {0}_CLS_NODE where CLSID = '{1}' and grade = 1 {2} order by syscode";
                result = Context.ExecuteSql<CLS_NODE>(string.Format(sql, DBName, categoryType, strwhere));

                if (result != null && result.Count > 0)
                {
                    foreach (CLS_NODE c in result)
                    {
                        c.Code = StringHelper.RemoveHighlightFlag(c.Code);
                        if (!String.IsNullOrEmpty(catcode))
                        {
                            string[] arrrystr = catcode.TrimStart('(').TrimEnd(')').Split(',');
                            int count = 0;
                            foreach (string m in arrrystr)
                            {
                                if (m.StartsWith(c.Code))//过滤有权限的子节点
                                {
                                    count = count + 1;
                                }
                            }
                            if (count > 1) // 
                            {
                                c.ChildFlag = 1;
                            }
                            else
                            {
                                c.ChildFlag = 0;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBName"></param>
        /// <param name="categoryType"></param>
        /// <returns></returns>
        public IList<CLS_NODE> GetCategroyListByCode(string DBName, int categoryType, string code)
        {
            IList<CLS_NODE> result = new List<CLS_NODE>();
            try
            {
                code = code.Replace(',', '+');
                string sql = "select CLSID,NAME,CODE,GRADE,CHILDFLAG,SYSCODE,SORTNUMBER,SYSID from {0}_CLS_NODE where CLSID = '{1}' and code = ({2}) order by syscode";
                result = Context.ExecuteSql<CLS_NODE>(string.Format(sql, DBName, categoryType, code));
                if (result != null && result.Count > 0)
                {
                    foreach (CLS_NODE c in result)
                    {
                        c.Code = StringHelper.RemoveHighlightFlag(c.Code);
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBName"></param>
        /// <param name="categoryType"></param>
        /// <returns></returns>
        public IList<CLS_NODE> GetCategroyListBySysCode(string DBName, int categoryType, string sysCode)
        {
            IList<CLS_NODE> result = new List<CLS_NODE>();
            try
            {
                sysCode = sysCode.Replace(',', '+');
                string sql = "select CLSID,NAME,CODE,GRADE,CHILDFLAG,SYSCODE,SORTNUMBER,SYSID from {0}_CLS_NODE where CLSID = '{1}' and syscode = {2} order by syscode";
                result = Context.ExecuteSql<CLS_NODE>(string.Format(sql, DBName, categoryType, sysCode));
                if (result != null && result.Count > 0)
                {
                    foreach (CLS_NODE c in result)
                    {
                        c.SysCode = StringHelper.RemoveHighlightFlag(c.SysCode);
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 获取所有分类节点
        /// </summary>
        /// <param name="DBName"></param>
        /// <param name="categoryType"></param>
        /// <returns></returns>
        public IList<CLS_NODE> GetCategroyAllList(string DBName, int categoryType, int DBID)
        {
            IList<CLS_NODE> result = new List<CLS_NODE>();
            try
            {
                string sql = "select CLSID,NAME,CODE,GRADE,CHILDFLAG,SYSCODE,SORTNUMBER,SYSID from {0}_CLS_NODE where CLSID = '{1}' order by syscode";
                result = Context.ExecuteSql<CLS_NODE>(string.Format(sql, DBName, categoryType), maxLength: 100000, dataCacheTimeout: ConfigHelper.GetCategoryCacheTimeout());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBName"></param>
        /// <param name="categoryType"></param>
        /// <returns></returns>
        public IList<CLS_NODE> GetChildrenCategroyList(string DBName, int categoryType, string code, int grade, int DBID)
        {
            IList<CLS_NODE> result = new List<CLS_NODE>();
            try
            {

                string strwhere = "";
                string catcode = null;
                if (SessionHelper.IsUserInfoExsit())
                {
                    catcode = GetNavChildCode(categoryType.ToString(), DBID);
                    if (catcode != null && catcode != "")
                    {
                        if ("-1" == catcode)
                        {
                            return result;
                        }
                        strwhere = " and CODE=" + catcode.Replace(',', '+'); //权限
                    }
                }


                string sql = "select CLSID,NAME,CODE,GRADE,CHILDFLAG,SYSCODE,SORTNUMBER,SYSID from {0}_CLS_NODE where CLSID = '{1}' and code = '{2}?' and grade = {3} {4} order by syscode";
                result = Context.ExecuteSql<CLS_NODE>(string.Format(sql, DBName, categoryType, code, grade + 1, strwhere));
                if (result != null && result.Count > 0)
                {
                    foreach (CLS_NODE c in result)
                    {
                        c.Code = StringHelper.RemoveHighlightFlag(c.Code);
                        if (!String.IsNullOrEmpty(catcode))
                        {
                            string[] arrrystr = catcode.TrimStart('(').TrimEnd(')').Split(',');
                            int count = 0;
                            foreach (string m in arrrystr)
                            {
                                if (m.StartsWith(c.Code))//过滤有权限的子节点
                                {
                                    count = count + 1;
                                }
                            }
                            if (count > 1) // 
                            {
                                c.ChildFlag = 1;
                            }
                            else
                            {
                                c.ChildFlag = 0;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE_CLS> GetDatabaseCLSType()
        {
            IList<SYS_CMS_DATABASE_CLS> result = null;
            try
            {
                //Expression<Func<SYS_CMS_DATABASE_CLS, bool>> query = c => (c.SysID == 7);
                result = Context.GetAll<SYS_CMS_DATABASE_CLS>();
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="clsID"></param>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE_CLS_NODE> GetDatabaseCLSNodeType(int clsID)
        {
            IList<SYS_CMS_DATABASE_CLS_NODE> result = null;
            try
            {
                //Expression<Func<SYS_CMS_DATABASE_CLS_NODE, bool>> query = c => (c.ClsID == clsID && c.Grade == 1);
                ExpressionCombine<SYS_CMS_DATABASE_CLS_NODE> exp = new ExpressionCombine<SYS_CMS_DATABASE_CLS_NODE>();
                exp.And("ClsID", OperatorType.Equal, clsID);
                exp.And("Grade", OperatorType.Equal, 1);
                result = Context.Get<SYS_CMS_DATABASE_CLS_NODE>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="clsID"></param>
        /// <param name="clsCode"></param>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE_CLS_DB> GetDatabaseCLSDBType(int clsID, IEnumerable<string> clsCode)
        {
            IList<SYS_CMS_DATABASE_CLS_DB> result = null;
            try
            {
                if (clsCode.Count() == 0)
                {
                    return new List<SYS_CMS_DATABASE_CLS_DB>();
                }
                ExpressionCombine<SYS_CMS_DATABASE_CLS_DB> subExp = new ExpressionCombine<SYS_CMS_DATABASE_CLS_DB>();
                foreach (string item in clsCode)
                {
                    subExp.Or("clsCode", OperatorType.Equal, item);
                }
                ExpressionCombine<SYS_CMS_DATABASE_CLS_DB> exp = new ExpressionCombine<SYS_CMS_DATABASE_CLS_DB>();
                exp.And("ClsID", OperatorType.Equal, clsID);
                exp.And(subExp);
                result = Context.Get<SYS_CMS_DATABASE_CLS_DB>(exp.Compile());
                foreach (SYS_CMS_DATABASE_CLS_DB item in result)
                {
                    item.ClsCode = StringHelper.RemoveHighlightFlag(item.ClsCode);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 根据code 获取数据库实例
        /// </summary>
        /// <param name="dbCode"></param>
        /// <param name="flage">1,过滤权限 0不过滤任何权限直接查询</param>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE> GetDatabaseByCode(IEnumerable<string> databaseCode, int flage = 1, PermissionOption option = PermissionOption.FilteringPermissions)
        {
            IList<SYS_CMS_DATABASE> result = null;
            try
            {
                if (databaseCode.Count() == 0)
                {
                    return new List<SYS_CMS_DATABASE>();
                }
                ExpressionCombine<SYS_CMS_DATABASE> exp = new ExpressionCombine<SYS_CMS_DATABASE>();
                if (flage == 1)
                {
                    exp.And("DatabasePubFlag", OperatorType.Equal, 1);
                    exp.And("VisibleFlag", OperatorType.Equal, 1);

                    ExpressionCombine<SYS_CMS_DATABASE> subExp1 = new ExpressionCombine<SYS_CMS_DATABASE>();
                    foreach (string item in databaseCode)
                    {
                        subExp1.Or("DatabaseCode", OperatorType.Equal, item);
                    }
                    exp.And(subExp1);

                    if (SessionHelper.IsUserInfoExsit() && option == PermissionOption.FilteringPermissions)
                    {
                        ExpressionCombine<SYS_CMS_DATABASE> subExp2 = new ExpressionCombine<SYS_CMS_DATABASE>();
                        IList<int> userDBInfo = SessionHelper.GetUserDBRight();
                        if (userDBInfo != null && userDBInfo.Count > 0)
                        {
                            foreach (int item in userDBInfo)
                            {
                                subExp2.Or("SysID", OperatorType.Equal, item);
                            }
                            exp.And(subExp2);
                        }

                        IList<string> securityLevel = SessionHelper.GetUserSecurity();
                        if (securityLevel != null && securityLevel.Count > 0)
                        {
                            ExpressionCombine<SYS_CMS_DATABASE> subExp3 = new ExpressionCombine<SYS_CMS_DATABASE>();
                            foreach (string security in securityLevel)
                            {
                                subExp3.Or("Secrity", OperatorType.Equal, security);
                            }
                            exp.And(subExp3);
                        }
                    }
                }
                else
                {
                    ExpressionCombine<SYS_CMS_DATABASE> subExp1 = new ExpressionCombine<SYS_CMS_DATABASE>();
                    foreach (string item in databaseCode)
                    {
                        subExp1.Or("DatabaseCode", OperatorType.Equal, item);
                    }
                    exp.And(subExp1);
                }
                Expression<Func<SYS_CMS_DATABASE, bool>> order = c => c.SortSn.Asc();
                result = Context.Get<SYS_CMS_DATABASE>(exp.Compile(), order);

                foreach (SYS_CMS_DATABASE item in result)
                {
                    item.DatabaseCode = StringHelper.RemoveHighlightFlag(item.DatabaseCode);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="databaseCode"></param>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE> GetDatabaseByID(IEnumerable<int> dbID)
        {
            IList<SYS_CMS_DATABASE> result = null;
            try
            {
                if (dbID.Count() == 0)
                {
                    return new List<SYS_CMS_DATABASE>();
                }

                ExpressionCombine<SYS_CMS_DATABASE> exp = new ExpressionCombine<SYS_CMS_DATABASE>();
                exp.And("DatabasePubFlag", OperatorType.Equal, 1);
                exp.And("VisibleFlag", OperatorType.Equal, 1);

                ExpressionCombine<SYS_CMS_DATABASE> subExp1 = new ExpressionCombine<SYS_CMS_DATABASE>();
                foreach (int item in dbID)
                {
                    subExp1.Or("SysID", OperatorType.Equal, item);
                }
                exp.And(subExp1);

                if (SessionHelper.IsUserInfoExsit())
                {
                    ExpressionCombine<SYS_CMS_DATABASE> subExp2 = new ExpressionCombine<SYS_CMS_DATABASE>();
                    IList<int> userDBInfo = SessionHelper.GetUserDBRight();
                    if (userDBInfo != null && userDBInfo.Count > 0)
                    {
                        foreach (int item in userDBInfo)
                        {
                            subExp2.Or("SysID", OperatorType.Equal, item);
                        }
                        exp.And(subExp2);
                    }

                    IList<string> securityLevel = SessionHelper.GetUserSecurity();
                    if (securityLevel != null && securityLevel.Count > 0)
                    {
                        ExpressionCombine<SYS_CMS_DATABASE> subExp3 = new ExpressionCombine<SYS_CMS_DATABASE>();
                        foreach (string security in securityLevel)
                        {
                            subExp3.Or("Secrity", OperatorType.Equal, security);
                        }
                        exp.And(subExp3);
                    }
                }

                result = Context.Get<SYS_CMS_DATABASE>(exp.Compile());

                foreach (SYS_CMS_DATABASE item in result)
                {
                    item.DatabaseCode = StringHelper.RemoveHighlightFlag(item.DatabaseCode);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="databaseId"></param>
        /// <returns></returns>
        public SYS_CMS_DATABASE GetDatabaseByID(int databaseId)
        {
            SYS_CMS_DATABASE result = null;
            try
            {
                ExpressionCombine<SYS_CMS_DATABASE> exp = new ExpressionCombine<SYS_CMS_DATABASE>();
                exp.And("SYSID", OperatorType.Equal, databaseId);
                IList<SYS_CMS_DATABASE> content = Context.Get<SYS_CMS_DATABASE>(exp.Compile());
                if (content != null && content.Count > 0)
                {
                    result = content[0];
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        public IList<SYS_CMS_DATABASE> GetDatabaseByTemplateID(IEnumerable<string> templateID)
        {
            IList<SYS_CMS_DATABASE> result = null;
            try
            {
                if (templateID.Count() == 0)
                {
                    return new List<SYS_CMS_DATABASE>();
                }

                ExpressionCombine<SYS_CMS_DATABASE> exp = new ExpressionCombine<SYS_CMS_DATABASE>();
                exp.And("DatabasePubFlag", OperatorType.Equal, 1);
                exp.And("VisibleFlag", OperatorType.Equal, 1);

                ExpressionCombine<SYS_CMS_DATABASE> subExp1 = new ExpressionCombine<SYS_CMS_DATABASE>();
                foreach (string item in templateID)
                {
                    subExp1.Or("TemplateID", OperatorType.Equal, item);
                }
                exp.And(subExp1);

                if (SessionHelper.IsUserInfoExsit())
                {
                    ExpressionCombine<SYS_CMS_DATABASE> subExp2 = new ExpressionCombine<SYS_CMS_DATABASE>();
                    IList<int> userDBInfo = SessionHelper.GetUserDBRight();
                    if (userDBInfo != null && userDBInfo.Count > 0)
                    {
                        foreach (int item in userDBInfo)
                        {
                            subExp2.Or("SysID", OperatorType.Equal, item);
                        }
                        exp.And(subExp2);
                    }

                    IList<string> securityLevel = SessionHelper.GetUserSecurity();
                    if (securityLevel != null && securityLevel.Count > 0)
                    {
                        ExpressionCombine<SYS_CMS_DATABASE> subExp3 = new ExpressionCombine<SYS_CMS_DATABASE>();
                        foreach (string security in securityLevel)
                        {
                            subExp3.Or("Secrity", OperatorType.Equal, security);
                        }
                        exp.And(subExp3);
                    }
                }

                result = Context.Get<SYS_CMS_DATABASE>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 获取有权限的分类导航名称
        /// </summary>
        /// <param name="DBID"></param>
        /// <returns></returns>
        private string GetNavFirstName(int DBID)
        {
            StringBuilder navfirstID = new StringBuilder();
            IList<string> arrayall = GetNav(DBID);
            if (null == arrayall)
            {
                return null;
            }
            if (arrayall != null && arrayall.Count > 0)
            {
                foreach (string str in arrayall)
                {
                    string[] secodstr = str.Split(':');
                    navfirstID.Append(secodstr[0]).Append(',');//截取导航分类的Name;
                }
            }
            else
            {
                return "-1";
            }
            return navfirstID.ToString();
        }
        /// <summary>
        /// 获取有权限的分类下的子类
        /// </summary>
        /// <param name="ClSID"></param>
        /// <param name="DBID"></param>
        /// <returns></returns>
        private string GetNavChildCode(string ClSID, int DBID)
        {
            string result = "";
            try
            {
                result = parserCLSInfo(DBID, int.Parse(ClSID));
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        private IList<string> GetNav(int DBID)
        {
            IList<string> result = new List<string>();
            try
            {
                string naviInfo = SessionHelper.GetUserCLS(DBID);
                if (null == naviInfo)
                {
                    return null;
                }
                if ("".Equals(naviInfo))
                {
                    return result;
                }
                MatchCollection mc;
                Regex r = new Regex("[^|;].+?:\\d+((\\(.+?\\))|;|$)"); //定义一个Regex对象实例
                mc = r.Matches(naviInfo);
                for (int i = 0; i < mc.Count; i++) //在输入字符串中找到所有匹配
                {
                    result.Add(mc[i].Value.TrimStart(';').TrimEnd(';'));
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        private string parserCLSInfo(int dbid, int targetclsID)
        {
            string result = null;
            try
            {
                IList<string> clsList = GetNav(dbid);
                if (null == clsList)
                {
                    return null;
                }
                if (clsList.Count == 0)
                {
                    return "-1";
                }
                string clsID = null;
                string clsCode = null;
                string[] clsInfo = null;
                string cls = null;
                for (int i = 0; i < clsList.Count; i++) //在输入字符串中找到所有匹配
                {
                    clsInfo = clsList[i].Split(':'); //将匹配的字符串添在字符串数组中
                    clsID = "";
                    clsCode = "";
                    if (clsInfo.Length > 1)
                    {
                        cls = clsInfo[1];
                        if (cls.Contains("("))
                        {
                            clsID = cls.Substring(0, cls.IndexOf('(')).TrimStart(';');
                            clsCode = cls.Substring(cls.IndexOf('(') + 1, cls.IndexOf(')') - cls.IndexOf('(') - 1);
                        }
                        else
                        {
                            clsID = cls.TrimEnd(';');
                        }
                    }

                    if (int.Parse(clsID) == targetclsID)
                    {
                        if (null == result)
                        {
                            result = "";
                        }
                        if ("".Equals(clsCode))
                        {
                            result = clsCode;
                            break;
                        }
                        else
                        {
                            result = result + "," + clsCode;
                        }
                    }
                }
                if (result != null && result.Length > 0)
                {
                    result = result.TrimStart(',');
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 删除导航分类
        /// </summary>
        /// <param name="sysid"></param>
        /// <param name="dbCode"></param>
        /// <returns></returns>
        public bool DelCLS(int sysid, string dbCode)
        {
            bool result = false;
            try
            {
                string sql = "delete from {0}_CLS where SysID={1}";
                sql = string.Format(sql, dbCode, sysid);
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="sysid"></param>
        /// <param name="dbCode"></param>
        /// <returns></returns>
        public bool DelCLS_Node(int sysid, string dbCode)
        {
            bool result = false;
            try
            {
                string sql = "delete from {0}_CLS_NODE where SysID={1}";
                sql = string.Format(sql, dbCode, sysid);
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBName"></param>
        /// <returns></returns>
        public bool InsertCategroyType(string dbCode, IList<CLS> categoryTypeList)
        {
            bool result = false;
            try
            {
                foreach (CLS item in categoryTypeList)
                {
                    string sql = "INSERT INTO {0}_CLS (TEMPLATECODE,NAME,EXPRESSION,AHEADMATCH,REFCLASSFLAG,SORTNUMBER) VALUES({1})";
                    StringBuilder sb = new StringBuilder();
                    sb.Append("'").Append(item.Templatecode).Append("','").Append(item.Name).Append("','").Append(item.Expression).Append("','").Append(item.AHeadMatch).Append("','").Append(item.RefClassFlag).Append("','").Append(item.SortNumber).Append("'");
                    sql = string.Format(sql, dbCode, sb.ToString());
                    result = Context.ExecuteNonQuery(sql);
                    if (!result)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBName"></param>
        /// <returns></returns>
        public bool InsertCategroyNode(string dbCode, IList<CLS_NODE> categoryTypeList)
        {
            bool result = false;
            try
            {
                foreach (CLS_NODE item in categoryTypeList)
                {
                    string sql = "INSERT INTO {0}_CLS_NODE (CLSID,NAME,CODE,GRADE,CHILDFLAG,SYSCODE,SORTNUMBER) VALUES({1})";
                    StringBuilder sb = new StringBuilder();
                    sb.Append("'").Append(item.ClsID).Append("','").Append(item.Name).Append("','").Append(item.Code).Append("','").Append(item.Grade).Append("','").Append(item.ChildFlag).Append("','").Append(item.SysCode).Append("','").Append(item.SortNumber).Append("'");
                    sql = string.Format(sql, dbCode, sb.ToString());
                    result = Context.ExecuteNonQuery(sql);
                    if (!result)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBName"></param>
        /// <returns></returns>
        public bool UpdateCategroyType(string dbCode, string sysid, Dictionary<string, object> content)
        {
            bool result = false;
            try
            {
                string sql = "UPDATE {0}_CLS SET {1} WHERE SYSID={2}";
                StringBuilder sb = new StringBuilder();
                foreach (string item in content.Keys)
                {
                    sb.Append(item + "='" + content[item] + "',");
                }
                string fields = sb.ToString();
                if (!string.IsNullOrEmpty(fields))
                {
                    fields = fields.Substring(0, fields.Length - 1);
                }
                sql = string.Format(sql, dbCode, fields, sysid);
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBName"></param>
        /// <returns></returns>
        public bool UpdateCategroyNode(string dbCode, string sysid, Dictionary<string, object> content)
        {
            bool result = false;
            try
            {
                string sql = "UPDATE {0}_CLS_NODE SET {1} WHERE SYSID={2}";
                StringBuilder sb = new StringBuilder();
                foreach (string item in content.Keys)
                {
                    sb.Append(item + "='" + content[item] + "',");
                }
                string fields = sb.ToString();
                if (!string.IsNullOrEmpty(fields))
                {
                    fields = fields.Substring(0, fields.Length - 1);
                }
                sql = string.Format(sql, dbCode, fields, sysid);
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public CLS GetCLSInfo(string dbCode, string expression)
        {
            CLS result = new CLS();
            try
            {
                string strwhere = "where EXPRESSION = '" + expression + "'";

                string sql = "select TEMPLATECODE,NAME,EXPRESSION,AHEADMATCH,REFCLASSFLAG,SORTNUMBER,SYSID from {0}_CLS {1} order by SORTNUMBER";
                IList<CLS> content = Context.ExecuteSql<CLS>(string.Format(sql, dbCode, strwhere));
                if (content != null && content.Count > 0)
                {
                    foreach (CLS c in content)
                    {
                        c.Name = StringHelper.RemoveHighlightFlag(c.Name);
                    }
                    result = content[0];
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 获得数据库信息---LZG添加
        /// </summary>
        /// <param name="dbcode"></param>
        /// <returns></returns>
        public SYS_CMS_DATABASE GetDatabaseByCode(string dbcode)
        {
            SYS_CMS_DATABASE result = null;
            try
            {
                string sql = @"select * from SYS_CMS_DATABASE where  DATABASECODE='{0}'";
                IList<SYS_CMS_DATABASE> itemList = Context.ExecuteSql<SYS_CMS_DATABASE>(string.Format(sql, dbcode));
                if (itemList != null && itemList.Count > 0)
                {
                    result = itemList[0];
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 获得数据库信息---LZG添加
        /// </summary>
        /// <param name="dbcode"></param>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE> GetDatabaseBySearchType(SearchType searchType)
        {
            IList<SYS_CMS_DATABASE> result = null;
            try
            {
                ExpressionCombine<SYS_CMS_DATABASE> exp = new ExpressionCombine<SYS_CMS_DATABASE>();
                exp.And("SearchType", OperatorType.Equal, (int)searchType);
                result = Context.Get<SYS_CMS_DATABASE>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public IList<SYS_CMS_DATABASE> GetDatabaseBySearchType(IList<SearchType> searchType)
        {
            IList<SYS_CMS_DATABASE> result = null;
            try
            {
                if (searchType != null && searchType.Count > 0)
                {
                    ExpressionCombine<SYS_CMS_DATABASE> exp = new ExpressionCombine<SYS_CMS_DATABASE>();
                    foreach (SearchType item in searchType)
                    {
                        exp.Or("SearchType", OperatorType.Equal, (int)item);
                    }
                    result = Context.Get<SYS_CMS_DATABASE>(exp.Compile());
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
    }
}
